package ltd.newbee.mall.api.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import ltd.newbee.mall.api.admin.param.BatchIdParam;
import ltd.newbee.mall.api.admin.param.GoodsAddParam;
import ltd.newbee.mall.api.admin.param.VideosAddParam;
import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.config.annotation.TokenToAdminUser;
import ltd.newbee.mall.entity.AdminUserToken;
import ltd.newbee.mall.entity.Video;
import ltd.newbee.mall.service.VideoService;
import ltd.newbee.mall.util.BeanUtil;
import ltd.newbee.mall.util.PageQueryUtil;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: Zcq
 * @create: 2022-09-14 08:41
 **/
@RestController
@Api(value = "v1", tags = "8-9.后台管理系统视频管理模块接口")
@RequestMapping("/manage-api/v1")
public class VideoAPI {
    @Autowired
    VideoService videoService;

    @RequestMapping(value = "/videos", method = RequestMethod.POST)
    @ApiOperation(value = "新增视频信息", notes = "新增视频信息")
    public Result addVideo(@RequestBody @Valid VideosAddParam VideosAddParam){
        Video video = new Video();
        BeanUtil.copyProperties(VideosAddParam,video);
        String result = videoService.addVideo(video);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }
    @RequestMapping(value = "/videos", method = RequestMethod.PUT)
    @ApiOperation(value = "修改视频信息", notes = "修改视频信息")
    public Result updateVideo(@RequestBody Video video){
        String result = videoService.upadateVideo(video);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }
    @RequestMapping(value = "/videos", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除视频信息", notes = "删除视频信息")
    public Result updateVideo(@RequestBody BatchIdParam batchIdParam){
        if (batchIdParam == null || batchIdParam.getIds().length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        if (videoService.deleteBatch(batchIdParam.getIds())) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult("删除失败");
        }
    }
    @RequestMapping(value = "/videos", method = RequestMethod.GET)
    @ApiOperation(value = "视频列表", notes = "视频列表")
    public Result list(@RequestParam(required = false) @ApiParam(value = "页码") Integer pageNumber,
                       @RequestParam(required = false) @ApiParam(value = "每页条数") Integer pageSize) {
        if (pageNumber == null || pageNumber < 1 || pageSize == null || pageSize < 10) {
            return ResultGenerator.genFailResult("分页参数异常！");
        }
        Map params = new HashMap(4);
        params.put("page", pageNumber);
        params.put("limit", pageSize);
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(videoService.getVideoPage(pageUtil));
    }

}
